package com.vast.ui.lcdpanel.views;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

import com.vast.ui.lcdpanel.models.SwtImageLayer;

public class SwtImageRenderer implements MouseListener {
	private Display    display;
	private Rectangle  viewPort;
	private MouseEvent mouseStatus;
	private final      Canvas canvas;
	private final      List<SwtImageLayer> imageLayerList;
	
	public SwtImageRenderer(final Composite parent) {
		this.display = parent.getDisplay();
		this.canvas = new Canvas(parent, SWT.NO_BACKGROUND);
		this.viewPort = this.canvas.getClientArea();
		createPaintListener();
		setClippingArea(0, 0, 320, 240);
		this.imageLayerList = new ArrayList<SwtImageLayer>();
		this.canvas.addMouseListener(this);
	}
	
	public List<SwtImageLayer> getList(){
		return this.imageLayerList;
	}
	
	public void setVisible(int index, boolean isVisible) {
		if(index > this.imageLayerList.size()) return;
		this.imageLayerList.get(index).setVisible(isVisible);
	}
	
	/**
	 * Sets the area of the Area where can be changed
	 * by drawing operations to the rectangular area specified
	 * by the argument. 
	 *
	 * @param x the x coordinate of the origin of the rectangle
	 * @param y the y coordinate of the origin of the rectangle
	 * @param width the width of the rectangle
	 * @param height the height of the rectangle
	 */
	public void setClippingArea(int x, int y, int width, int height) {
		this.viewPort = new Rectangle(x, y, width, height);
	}
	
	public void add(SwtImageLayer imageLayer) {
		imageLayer.setLayerNo(this.imageLayerList.size());
		this.imageLayerList.add(imageLayer);
	}
	
	public void drawLayer(PaintEvent e) {
		e.gc.setClipping(this.viewPort);
	
		for(SwtImageLayer layer: this.imageLayerList) {
			if(layer.isVisible())
				e.gc.drawImage(layer.getImage(), layer.getX(), layer.getY());
		}
		
	}
	
	public void createPaintListener() {
		this.canvas.addPaintListener(new PaintListener() {
	      public void paintControl(PaintEvent e) {
	    	  drawLayer(e);
	      }
	   });
	}

	public void redraw() {
		this.canvas.redraw();
	}
	
	public void setLocation(int index, int x, int y) {
		this.imageLayerList.get(index).setLocation(x, y);
	}
	
	public void updateMouseStatus(MouseEvent e) {
		this.mouseStatus = e;
	}
	
	public void mouseDoubleClick(MouseEvent e) {
		// TODO Auto-generated method stub
		updateMouseStatus(e);	
	}

	public void mouseDown(MouseEvent e) {
		// TODO Auto-generated method stub
		updateMouseStatus(e);
		this.imageLayerList.get(1).setLocation(e.x, e.y);
		this.canvas.redraw();
	}

	public void mouseUp(MouseEvent e) {
		// TODO Auto-generated method stub
		updateMouseStatus(e);
		
	}
}
